function format_flag(cellvalue, options, rowObject)
{
	if(cellvalue == 'S')
	{
		return "Sim";
	}
	else
	{
		return "N&atilde;o";
	}
}

function format_tipo_conta(cellvalue, options, rowObject)
{
	if(cellvalue == 'R')
	{
		return "a Receber";
	}
	else
	{
		return "a Pagar";
	}
}

function format_status_conta(cellvalue, options, rowObject)
{
	if(cellvalue == 'A')
	{
		return "Aberta";
	}
	else
	{
		return "Paga";
	}
}

function flag_tipo_conta(cellvalue, options, rowObject)
{
	switch(cellvalue)
	{
		case "CL":
			return "Cliente Center Telecom";
		break;
		case "CO":
			return "Concorrente";
		break;
		case "PA":
			return "Parceiro";
		break;
		case "RE":
			return "Representante";
		break;
		case "CT":
			return "Cliente TIM";
		break;
	}
}

function fc_get_fields(arr_fields)
{
	var fields = "var arr_data = {";
	for(i=0; i<arr_fields.length; i++)
	{
		if(arr_fields[i].tp_obj == "txt" || arr_fields[i].tp_obj == "hid")
		{
			if($("#"+arr_fields[i].nome).val() != undefined)
			{
				fields += arr_fields[i].nome+":\""+$("#"+arr_fields[i].nome).val()+"\",";
			}
		}
		else if(arr_fields[i].tp_obj == "sel")
		{
			fields += arr_fields[i].nome+":\""+fc_get_val_sel(arr_fields[i].nome)+"\",";
		}
		else if(arr_fields[i].tp_obj == "opt")
		{
			fields += arr_fields[i].nome+":\""+fc_get_val_opt(arr_fields[i].nome)+"\",";
		}
		else if(arr_fields[i].tp_obj == "chk")
		{
			fields += arr_fields[i].nome+":\"";
			$('#'+arr_fields[i].div_name+' input[name="'+arr_fields[i].nome+'"]:checked').each(function() 
			{
				fields += $(this).val()+"##";
			});
			fields += "\",";
		}
	}
	fields = fields.substring(0, (fields.length - 1)) + "}";
	return fields;
}

function fc_reset_form(arr_fields)
{
	for(i=0; i<arr_fields.length; i++)
	{
		if(arr_fields[i].tp_obj == "txt" || arr_fields[i].tp_obj == "hid")
		{
			$("#"+arr_fields[i].nome).val("");
		}
		else if(arr_fields[i].tp_obj == "sel")
		{
			document.getElementById(arr_fields[i].nome).selectedIndex = 0;
		}
		else if(arr_fields[i].tp_obj == "opt")
		{
			//fields += arr_fields[i].nome+":\""+fc_get_val_opt(arr_fields[i].nome)+"\",";
		}
		else if(arr_fields[i].tp_obj == "chk")
		{
			$('#'+arr_fields[i].div_name+' input[name="'+arr_fields[i].nome+'"]:checked').each(function() 
			{
				$('input[name='+arr_fields[i].nome+']').attr('checked', false);
				$(this).checked = false;
			});
		}
	}	
}

function fc_get_val_sel(str_id)
{
    obj_sel = document.getElementById(str_id);
    
    if (!obj_sel)
    {
        alert("Objeto nao encontrado: " + str_id);
        return;
    }
    
    return obj_sel.options[obj_sel.selectedIndex].value;
}

function fc_get_val_opt(str_id)
{
    if (!document.getElementById(str_id + "_s"))
    {
        alert("Object not found: " + str_id);
        return; 
    }            

    if (document.getElementById(str_id + "_s").checked)
    {
        return document.getElementById(str_id + "_s").value;
    }
    else if (document.getElementById(str_id + "_n").checked)
    {
        return document.getElementById(str_id + "_n").value;
    }
    else
    {
        return "";
    }
}

function fc_edit(arr_fields, obj)
{
	eval("var data ="+obj);
	for(i=0; i<arr_fields.length-1; i++)
	{
		var item = data.rows[0].cell[i];
		if (item != null)
		{
			item = item.substring(0, 3);
			if(item == "chk")
			{
				var chk = $("."+arr_fields[i].nome).get();
				var arr_res = data.rows[0].cell[i].split("##");
				for(x=0; x < chk.length; x++)
				{
					for(y=0; y<arr_res.length; y++)
					{
						if(chk[x].value == arr_res[y])
						{
							chk[x].checked = true;
						}
					}
				}
			}
			else
			{
				$("#"+arr_fields[i].nome).val(data.rows[0].cell[i]);
			}
		}
	}
}

function dialogMessage(msg,divMsg)
{
	document.getElementById(divMsg).display= '';
	document.getElementById(divMsg).innerHTML= msg;
	$("#"+divMsg).dialog({
		modal: true,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});	
}

function updateTips(t, tips) 
{
	tips
		.text(t)
		.addClass('ui-state-highlight');
	setTimeout(function() {
		tips.removeClass('ui-state-highlight', 1500000);
	}, 50000);
}

function checkLength(o,n,tips,min,max) 
{
	if ( o.val().length > max || o.val().length < min ) {
		o.addClass('ui-state-error');
		updateTips("O tamanho de " + n + " deve ser entre "+min+" e "+max+".", tips);
		return false;
	} else {
		return true;
	}
}

function checkNumber(obj, label, tips)
{
	var exp = /^\d+$/;
	if(obj.val() == null || obj.val() == "")
	{
		return true;
	}
	else
	{
		if(!(exp.test(obj.val())))
		{
			obj.addClass('ui-state-error');
			updateTips("O campo "+label+" n\u00e3o \u00e9 num\u00e9rico!", tips);
			return false;
		}
		else
		{
			return true;
		}
	}
}

function checkDecimal(obj, label, tips)
{
	var exp = /^[+-]?((\d+|\d{1,3}(\.\d{3})+)(\,\d*)?|\,\d+)$/;
	if(obj.val() == null || obj.val() == "")
	{
		return true;
	}
	else
	{
		if(!(exp.test(obj.val())))
		{
			obj.addClass('ui-state-error');
			updateTips("O campo "+label+" n\u00e3o \u00e9 um decimal v\u00e1lido!", tips);
			return false;
		}
		else
		{
			return true;
		}
	}
}

function checkMoney(obj, label, tips)
{
	var exp = /^\d{1,3}(\.\d{3})*\,\d{2}$/;
	if(obj.val() == null || obj.val() == "")
	{
		return true;
	}
	else
	{
		if(!(exp.test(obj.val())))
		{
			obj.addClass('ui-state-error');
			updateTips("O campo "+label+" n\u00e3o \u00e9 um monet\u00e1rio v\u00e1lido!", tips);
			return false;
		}
		else
		{
			return true;
		}
	}
}

function checkDate(obj, label, tips)
{
	var exp = /^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20)?\d{2}$/;
	if(obj.val() == null || obj.val() == "")
	{
		return true;
	}
	else
	{
		if(!(exp.test(obj.val())))
		{
			obj.addClass('ui-state-error');
			updateTips("O campo "+label+" n\u00e3o \u00e9 uma data v\u00e1lida!", tips);
			return false;
		}
		else
		{
			return true;
		}
	}
}

function checkEmail(obj, label, tips)
{
	var exp = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;;
	if(obj.val() == null || obj.val() == "")
	{
		return true;
	}
	else
	{
		if(!(exp.test(obj.val())))
		{
			obj.addClass('ui-state-error');
			updateTips("O campo "+label+" n\u00e3o \u00e9 um e-mail v\u00e1lido!", tips);
			return false;
		}
		else
		{
			return true;
		}	
	}
}

function checkRequired(obj, label, tips)
{
	if(obj.val() == null || obj.val() == "")
	{
		obj.addClass('ui-state-error');
		updateTips("O campo "+label+" \u00e9 obrigat\u00f3rio!", tips);
		return false;
	}
	else
	{
		return true;
	}
}

function validation(obj, label, type, tips, min, max)
{
	var ret = false;
	switch(type)
	{
		case 'number':
			ret = checkNumber(obj, label, tips);
			break;
		case 'decimal':
			ret = checkDecimal(obj, label, tips);
			break;
		case 'money':
			ret = checkMoney(obj, label, tips);
			break;
		case 'date':
			ret = checkDate(obj, label, tips);
			break;
		case 'email':
			ret = checkEmail(obj, label, tips);
			break;
		case 'length':
			ret = checkLength(obj, label, tips, min, max);
			break;
		case 'required':
			ret = checkRequired(obj, label, tips);
			break;
	}
	return ret;
}

function checkRegexp(o,regexp,n) 
{
	if ( !( regexp.test( o.val() ) ) ) {
		o.addClass('ui-state-error');
		updateTips(n);
		return false;
	} else {
		return true;
	}
}

